
<template>
  <el-dialog v-model="dialogFormVisible" class="dialog-title" :title="title">
    <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="120px" style="width: 90%; margin-left:50px;">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="任务名称" prop="jobName">
            <el-input v-model="temp.jobName" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="任务分组" prop="jobGroup">
            <el-input v-model="temp.jobGroup" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="加载方式" prop="loadWay">
            <el-radio
              v-for="item in dictList('load_way')"
              :key="'type' + item.label"
              v-model="temp.loadWay"
              :label="item.value"
            >
              {{ item.label }}
            </el-radio>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="执行类" prop="executeClass">
            <el-input v-model="temp.executeClass" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="方法名" prop="methodName">
            <el-input v-model="temp.methodName" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="方法参数" prop="methodParams">
            <el-input v-model="temp.methodParams" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="cron表达式" prop="cronExpression">
            <el-input v-model="temp.cronExpression" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="是否同步" prop="isConcurrent">
            <el-radio
              v-for="item in dictList('sf')"
              :key="'type' + item.label"
              v-model="temp.isConcurrent"
              :label="item.value"
            >
              {{ item.label }}
            </el-radio>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="任务描述">
        <el-input
          v-model="temp.templateContent"
          :autosize="{ minRows: 2, maxRows: 4}"
          type="textarea"
          placeholder="请输入任务描述"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="dialogFormVisible = false">取消</el-button>
      <el-button type="primary" :loading="loading" @click="title==='新增'?createData():updateData()">
        确定
      </el-button>
    </template>
  </el-dialog>
</template>
<script>
import { createSchedule, updateSchedule } from '@/api/tool/task/schedule'

export default {
  name: 'TaskListForm',
  emits: ['refreshList'],
  data() {
    return {
      temp: {
        id: undefined,
        jobName: '',
        cronExpression: '',
        executeClass: '',
        methodName: '',
        methodParams: '',
        misfirePolicy: '',
        loadWay: '1',
        isConcurrent: '1',
        description: '',
        jobStatus: '',
        jobGroup: ''
      },
      loading: false,
      dialogFormVisible: false,
      title: undefined,
      rules: {
        jobName: [{ required: true, message: '请输入任务名称！' }],
        jobGroup: [{ required: true, message: '请输入任务分组！' }],
        executeClass: [{ required: true, message: '请输入执行类！' }],
        loadWay: [{ required: true, message: '请选择加载方式！' }],
        isConcurrent: [{ required: true, message: '请选择是否同步！' }],
        methodName: [{ required: true, message: '请输入方法名！' }],
        cronExpression: [{ required: true, message: '请输入cron表达式！' }]
      }
    }
  },
  methods: {
    /**
     * 刷新列表
     */
    refreshList() {
      this.$emit('refreshList')
    },
    /**
     * 重置表单
     */
    resetTemp() {
      this.loading = false
      this.temp = {
        id: undefined,
        jobName: '',
        cronExpression: '',
        executeClass: '',
        methodName: '',
        methodParams: '',
        misfirePolicy: '',
        loadWay: '1',
        isConcurrent: '1',
        description: '',
        jobStatus: '',
        jobGroup: ''
      }
    },
    /**
     * 新增
     */
    handleCreate() {
      this.resetTemp()
      this.title = '新增'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs.dataForm.clearValidate()
      })
    },
    /**
     * 新增数据
     */
    createData() {
      this.$refs.dataForm.validate((valid) => {
        if (valid) {
          this.loading = true
          createSchedule(this.temp).then((response) => {
            this.loading = false
            const data = response.data
            if (!data.success) {
              this.$message({
                message: data.msg,
                type: 'error'
              })
            } else {
              this.dialogFormVisible = false
              this.$message.success('创建成功')
              this.refreshList()
            }
          })
        }
      })
    },
    /**
     * 修改
     * @param row 选中数据
     */
    handleUpdate(row) {
      this.temp = Object.assign({}, row) // copy obj
      this.temp.timestamp = new Date(this.temp.timestamp)
      this.title = '编辑'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs.dataForm.clearValidate()
      })
    },
    /**
     * 修改数据
     */
    updateData() {
      this.$refs.dataForm.validate((valid) => {
        if (valid) {
          this.loading = true
          const tempData = Object.assign({}, this.temp)
          tempData.timestamp = +new Date(tempData.timestamp) // change Thu Nov 30 2017 16:41:05 GMT+0800 (CST) to 1512031311464
          updateSchedule(tempData).then((response) => {
            this.loading = false
            const data = response.data
            if (!data.success) {
              this.$message({
                message: data.msg,
                type: 'error'
              })
            } else {
              this.dialogFormVisible = false
              this.$message.success('创建成功')
              this.refreshList()
            }
          })
        }
      })
    }
  }
}
</script>
<style scoped lang="scss">

</style>
